﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace vote
{
    public partial class back_editVote : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindVote();
            }
            if (gvVote.EditIndex >= 0)
            {
                addControlsToPlaceHolder();
            }

        }

        private void addControlsToPlaceHolder()
        {
            DropDownList ddl = gvVote.Rows[gvVote.EditIndex].Cells[1].FindControl("ddlOpCount") as DropDownList;
            PlaceHolder ph = gvVote.Rows[gvVote.EditIndex].Cells[1].FindControl("phOptions") as PlaceHolder;

            for (int i = 0; i < int.Parse(ddl.SelectedValue); i++)
            {
                Poll poll = new Poll();
                poll.OptionIndex = i + 1;
                ph.Controls.Add(poll);
            }
        }

        private void BindVote()
        {
            DataSet ds = AccessDBUtil.ExecuteQuery("select * from [voteTitle]");
            gvVote.DataSource = ds.Tables[0].DefaultView;
            gvVote.DataBind();
        }

        protected void gvVote_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvVote.EditIndex = e.NewEditIndex;
            BindVote();
        }
        protected void gvVote_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvVote.EditIndex = -1;
            BindVote();
        }
        
        protected void gvVote_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            List<string> listSqls = new List<string>();
            List<SqlParameter[]> listParams = new List<SqlParameter[]>();

            listSqls.Add("delete from [voteTitle] where [id] = @id");
            SqlParameter[] parameters = new SqlParameter[1];
            parameters[0] = new SqlParameter("@id", SqlDbType.Int);
            parameters[0].Value = gvVote.Rows[e.RowIndex].Cells[0].Text;
            listParams.Add(parameters);

            listSqls.Add("delete from [voteOption] where [parentId] = @pid");
            parameters = new SqlParameter[1];
            parameters[0] = new SqlParameter("@pid", SqlDbType.Int);
            parameters[0].Value = gvVote.Rows[e.RowIndex].Cells[0].Text;
            listParams.Add(parameters);

            AccessDBUtil.ExecuteTrans(listSqls, listParams);

        }

        protected void gvVote_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow )
            {
                if (e.Row.RowState == DataControlRowState.Edit || (e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit)))
                {
                    try
                    {
                        DropDownList ddl = e.Row.Cells[1].FindControl("ddlOpCount") as DropDownList;
                        string sql = "select [option],[ballots],[color] from [voteOption] where [parentId] = @pid";
                        SqlParameter[] parameters = new SqlParameter[1];
                        parameters[0] = new SqlParameter("@pid", SqlDbType.VarChar, 70);
                        parameters[0].Value = e.Row.Cells[0].Text;
                        DataSet dsOption = AccessDBUtil.ExecuteQuery(sql,parameters);
                        ddl.SelectedValue = dsOption.Tables[0].Rows.Count.ToString();

                        PlaceHolder ph = e.Row.Cells[1].FindControl("phOptions") as PlaceHolder;
                        for (int i = 1; i <= dsOption.Tables[0].Rows.Count; i++)
                        {
                            Poll option = new Poll();
                            option.OptionIndex = i;

                            option.OptionName = dsOption.Tables[0].Rows[i-1][0].ToString();
                            option.Ballots = int.Parse(dsOption.Tables[0].Rows[i - 1][1].ToString());
                            option.OptionColor = dsOption.Tables[0].Rows[i - 1][2].ToString();
                            ph.Controls.Add(option);
                        }

                        SqlDataReader rMode = AccessDBUtil.ExecuteReader("select [singleMode],[colMode] from [voteTitle] where [id]=" + e.Row.Cells[0].Text);
                        RadioButtonList selectMode = e.Row.Cells[1].FindControl("rblSelectPattern") as RadioButtonList;
                        RadioButtonList chartMode = e.Row.Cells[1].FindControl("rblPicStyle") as RadioButtonList;
                        if (rMode.Read())
                        {
                            selectMode.SelectedIndex = rMode.GetBoolean(0) ? 0 : 1;
                            chartMode.SelectedIndex = rMode.GetBoolean(1) ? 0 : 1;
                        }
                        rMode.Close();
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                    
                }//if
            }
        }

        protected void gvVote_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            List<string> listSqls = new List<string>();
            List<SqlParameter[]> listParams = new List<SqlParameter[]>();

            string id = gvVote.Rows[e.RowIndex].Cells[0].Text;
            //更新 投票主题 是否单选 图形模式 
            listSqls.Add("update [voteTitle] set [title] = @title , [singleMode] = @smode , [colMode] = @cmode where [id] = @id");
            SqlParameter[] parameters = new SqlParameter[4];
            parameters[0] = new SqlParameter("@title", SqlDbType.VarChar, 70);
            parameters[0].Value = ((TextBox)gvVote.Rows[e.RowIndex].Cells[1].FindControl("tbTitle")).Text.Trim();
            parameters[1] = new SqlParameter("@smode", SqlDbType.Bit);
            parameters[1].Value = ((RadioButtonList)gvVote.Rows[e.RowIndex].Cells[1].FindControl("rblSelectPattern")).Items[0].Selected;
            parameters[2] = new SqlParameter("@cmode", SqlDbType.Bit);
            parameters[2].Value = ((RadioButtonList)gvVote.Rows[e.RowIndex].Cells[1].FindControl("rblPicStyle")).Items[0].Selected;
            parameters[3] = new SqlParameter("@id", SqlDbType.Int);
            parameters[3].Value = id;
            listParams.Add(parameters);

            PlaceHolder ph = gvVote.Rows[e.RowIndex].Cells[1].FindControl("phOptions") as PlaceHolder;

            //更新在 PlaceHolder 中的选项
            for (int i = 0; i < ph.Controls.Count; i++)
            {
                string sql = string.Empty;

                if (i == 0)
                {
                    sql = @"update [voteOption] set [option] = @opt , [color] = @clr , [ballots] = @balts 
                            where [id] in (select top 1 [id] from [voteOption] where [parentId] = " + id + ")";
                }
                else
                {
                    sql = @"update [voteOption] set [option] = @opt , [color] = @clr , [ballots] = @balts 
                            where [id] in (select top 1 [id] from [voteOption] 
                            where [parentId] = " + id + " and [id] not in (select top "
                        + i + " [id] from [voteOption] where [parentId] = " + id + "))";
                }

                listSqls.Add(sql);

                Poll poll = (Poll)ph.Controls[i];
                parameters = new SqlParameter[3];
                parameters[0] = new SqlParameter("@opt", SqlDbType.VarChar, 70);
                parameters[0].Value = poll.OptionName;
                parameters[1] = new SqlParameter("@clr", SqlDbType.VarChar, 50);
                parameters[1].Value = poll.OptionColor;
                parameters[2] = new SqlParameter("@balts", SqlDbType.Int);
                parameters[2].Value = poll.Ballots;
                
                listParams.Add(parameters);
                
            }

            AccessDBUtil.ExecuteTrans(listSqls, listParams);

            gvVote.EditIndex = -1;
            BindVote();
        }
}
}
